package com.spring.sunflower.conversation;

import static com.spring.sunflower.base.BaseFragment.mStrangeListData;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseCompatActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.event.LastChatEvent;
import com.spring.sunflower.event.StrangeConversationChangedEvent;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMManager;

import org.greenrobot.eventbus.Subscribe;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/9/1<br/>
 * <p>
 * Describe:<br/>
 */
public class StrangeActivity extends BaseCompatActivity {
    private List<V2TIMConversation> mConversationList = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private MessageAdapter mAdapter;
    private LinearLayoutManager layoutManager;
    private int lastClickPos = -1;
    private LinearLayout llEmpty;

    @Override
    public int getLayoutID() {
        return R.layout.activity_strange;
    }

    @Override
    public void initView() {
        tvTitle.setText("陌生人消息");
        mRecyclerView = findViewById(R.id.rvRecyclerView);
        llEmpty = findViewById(R.id.llEmpty);
        layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new MessageAdapter(R.layout.adapter_message_fragment, mConversationList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setList(mStrangeListData);
        if (mStrangeListData.size() > 0) {
            llEmpty.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
        } else {
            llEmpty.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                lastClickPos = position;
                V2TIMConversation v2TIMConversation = mConversationList.get(position);
                String conversationID = v2TIMConversation.getConversationID();
                String userID = v2TIMConversation.getUserID();
                String showName = v2TIMConversation.getShowName();
                String draftText = v2TIMConversation.getDraftText();
                String faceUrl = v2TIMConversation.getFaceUrl();
                printD("conversationID=" + conversationID);
                Intent intent = new Intent(StrangeActivity.this, ChatActivity.class);
                intent.putExtra(Constant.IS_FROM_HELP, false);
                intent.putExtra(Constant.C2C_CONVERSATION_ID, conversationID);
                intent.putExtra(Constant.C2C_USER_ID, userID);
                intent.putExtra(Constant.C2C_SHOW_NAME, showName);
                intent.putExtra(Constant.C2C_DRAFT_TEXT, draftText);
                intent.putExtra(Constant.C2C_FACE_URL, faceUrl);
                startActivity(intent);
                V2TIMManager.getMessageManager().markC2CMessageAsRead(userID, new V2TIMCallback() {
                    @Override
                    public void onError(int code, String desc) {

                    }

                    @Override
                    public void onSuccess() {

                    }
                });
            }
        });
    }

    @Override
    public boolean shouldRegisterEventBus() {
        return true;
    }

    @Subscribe
    public void replaceStrangeData(StrangeConversationChangedEvent event) {
        String userID = event.getUserID();
        boolean conversationIsNull = event.isConversationIsNull();
        V2TIMConversation conversation = ConversationManager.getConversation(mStrangeListData, userID);
        if (conversation != null) {//不为null说明在陌生人里面
            int index = mStrangeListData.indexOf(conversation);
            if (conversation.isPinned()) {
                mAdapter.removeAt(index);
            } else {
                mAdapter.setList(mStrangeListData);
            }
        } else {//为null数据已经改变 刷新
//            mAdapter.notifyDataSetChanged();
//            if (lastClickPos != -1 && mAdapter.getItemCount() > lastClickPos) {
//                mAdapter.removeAt(lastClickPos);
//            }
        }
//        mAdapter.setList(mStrangeListData);
    }

    @Subscribe
    public void refreshConversationList(LastChatEvent event) {
        String userID = event.getUserID();
        //如果是陌生人消息就添加进去,并从陌生人消息中移除
        V2TIMConversation conversation = ConversationManager.getConversation(mStrangeListData, userID);
        if (conversation != null) {
            int index = mStrangeListData.indexOf(conversation);
            mAdapter.removeAt(index);
        }
        if (mAdapter.getItemCount() <= 0) {
            llEmpty.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }
    }
}
